<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/8/18
 * Time: 16:33
 */

namespace J\Schema\Collection;


use J\Schema\ICurd;

/**
 * 支持内部表
 *
 * @method int max($field = '*', $where = true, $model = null)
 * @method int min($field = '*', $where = true, $model = null)
 * @method int count($field = '*', $where = true, $model = null)
 * @method int sum($field = '*', $where = true, $model = null)
 */
class CurdExtra implements ICurd
{
    /**
     * @var ICurd
     */
    protected $main;

    protected $elements;

    /**
     * CurdExtra constructor.
     * @param ICurd $main
     *
     */
    public function __construct(ICurd $main)
    {
        $this->main = $main;
    }


    public function add($id = 0)
    {
        $id = $this->main->add();

        $this->main->getCollection()->inners()->iterateCollection(
            function ($collection) use ($id) {
                /** @var ICollection $collection */
                $collection->getCurd()->setElements($this->elements)->add($id);
            }
        );

        return $id;
    }

    public function edit()
    {
        $r = $this->main->edit();
        $ids = $this->main->getIds();

        $this->main->getCollection()->inners()->iterateCollection(
            function ($collection) use ($ids) {
                /** @var ICollection $collection */
                $collection->getCurd()->setIds($ids)->setElements($this->elements)->edit();
            }
        );

        return $r;
    }

    public function select()
    {
        $rs = $this->main->select();
        $ids = $this->main->getIds();

        $this->main->getCollection()->inners()->iterateCollection(
            function ($collection) use ($ids, &$rs) {
                /** @var ICollection $collection */
                $r = $collection->getCurd()->setIds($ids)->setElements($this->elements)->select();

                foreach ($r as $id => $line) {
                    $rs[$id] = array_merge($rs[$id], $line);
                }
            }
        );

        return $rs;
    }

    public function find()
    {
        $rs = $this->select();
        $r = array_values($rs);
        return $r[0];
    }

    public function clear()
    {
        $r = $this->main->clear();
        $ids = $this->main->getIds();

        $this->main->getCollection()->inners()->iterateCollection(
            function ($collection) use ($ids) {
                /** @var ICollection $collection */
                $collection->getCurd()->setIds($ids)->clear();
            }
        );

        return $r;
    }

    public function setElements($elements)
    {
        $this->elements = $elements;
        $this->main->setElements($elements);
        return $this;
    }

    public function delete()
    {
        return $this->main->delete();
    }

    public function recycle()
    {
        return $this->main->recycle();
    }

    public function setIds($ids = null)
    {
        $this->main->setIds($ids);
        return $this;
    }

    public function getIds()
    {
        return $this->main->getIds();
    }

    public function setRecycle($recyclable)
    {
        $this->main->setRecycle($recyclable);
        return $this;
    }

    public function isRecyclable()
    {
        return $this->main->isRecyclable();
    }

    public function setWhere($where)
    {
        $this->main->setWhere($where);
        return $this;
    }

    public function getWhere()
    {
       return $this->main->getWhere();
    }

    public function setOption($option)
    {
        $this->main->setOption($option);
        return $this;
    }

    public function getOption()
    {
        return $this->main->getOption();
    }

    public function setRaw($raw)
    {
        $this->main->setRaw($raw);
        return $this;
    }

    public function query($fetch_model = null)
    {
        return $this->main->query($fetch_model);
    }

    public function __call($name, $arguments)
    {
        return call_user_func_array([$this->main, $name], $arguments);
    }

    public function extra()
    {
        return $this;
    }

    public function relation()
    {
        $this->main = $this->main->relation();
        return $this;
    }

    public function getCollection()
    {
        return $this->main->getCollection();
    }
}